package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.ItemStageService;
import com.zerdoor.util.exception.InsufficientBalanceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目阶段控制层
 * Created by zhongjieyu on 2015/1/6.
 */
@Controller
@RequestMapping("/itemStage")
public class ItemStageController {


    @Autowired
    private ItemStageService itemStageService;

    /**
     * 根据项目合同ID查询阶段数据
     * @param userType 用户类型 0为艺术家 1为发包方
     * @return
     */
    @RequestMapping(value = "/findStageByContractId", method = RequestMethod.GET)
    public String findStageByContractId(int userType,int contractId,HttpServletRequest request) {


        try {


            //得到阶段数据
            List<Map<String,String>> list = itemStageService.findStageByContractId(contractId);

            request.setAttribute("stageList",list);
            request.setAttribute("contractId",contractId);

//            ResponseEntity<List<Map<String,String>>> responseEntity = new ResponseEntity<List<Map<String,String>>>(list, HttpStatus.OK);
//
//            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();
//
//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }

        //跳转到发包方阶段页面
        if(1 == userType){

            return "/chat/baileStage";
        }else{

            return "/chat/artistStage";

        }


    }

    /**
     * 保存阶段附件
     * @param deliverable 附件地址
     * @param stageId 阶段ID
     * @return
     */
    @RequestMapping(value = "/saveStageDeliverable", method = RequestMethod.POST)
    public ResponseEntity<?> saveStageDeliverable(String deliverable,String stageId) {


        try {


            itemStageService.updateStageDeliverableById(deliverable,stageId);


            ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);

            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();
//
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }



    }


    /**
     * 跳转到支付页面
     * @return
     */
    @RequestMapping(value = "/publishAndPay", method = RequestMethod.GET)
    public String publishAndPay(int stageId,HttpServletRequest request) {



        try {

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");


            Map payInfo =  itemStageService.findOrderInfoByStageId(stageId, userInfo.getUserId());


            request.setAttribute("payInfo",payInfo);
            request.setAttribute("stageId",stageId);

            return "/chat/publishAndPay";

        } catch (Exception e) {

            e.printStackTrace();
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return "/chat/publishAndPay";
        }




    }


    /**
     * 支付宝支付增值服务费用
     * @param stageId 任务信息ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByAlipay", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByAlipay(int stageId,HttpServletRequest request) {



        try {


            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = userInfo.getUserId();
            boolean flag = itemStageService.addvaluePayByAlipay(stageId,userID);



            ResponseEntity<Boolean> responseEntity = new ResponseEntity<Boolean>(flag, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }


    /**
     * paypay支付增值服务费用
     * @param stageId 任务信息ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByPaypay", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByPaypay(int stageId,HttpServletRequest request) {



        try {




            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = userInfo.getUserId();
            boolean flag = itemStageService.addvaluePayByPaypay(stageId,userID);



            ResponseEntity<Boolean> responseEntity = new ResponseEntity<Boolean>(flag, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }

    /**
     * 余额支付增值服务费用
     * @param stageId 任务信息ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByBalance", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByBalance(int stageId,HttpServletRequest request) {


        Map<String, Object> data = new HashMap<String, Object>();
        try {

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = userInfo.getUserId();

            data = itemStageService.addvaluePayByBalance(stageId,userID);

        }catch (InsufficientBalanceException e){

            data.put("static","1");
            data.put("msg","支付失败！余额不足或账户被冻结！");

        }catch (Exception e) {


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }


        ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<Map<String, Object>>(data, HttpStatus.OK);
        return responseEntity;

    }

    /**
     * 通过本阶段
     * @param stageId 阶段ID
     * @return
     */
    @RequestMapping(value = "/throughThisStage", method = RequestMethod.POST)
    public ResponseEntity<?> throughThisStage(int stageId,HttpServletRequest request) {


        String flag = "success";
        try {

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = userInfo.getUserId();

            boolean flag1 = itemStageService.updateThroughThisStage(stageId, userID);

            if(!flag1){
                flag = "lose";
            }

        }catch (Exception e) {

            e.printStackTrace();
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }


        ResponseEntity<String> responseEntity = new ResponseEntity<String>(flag, HttpStatus.OK);
        return responseEntity;

    }


}
